package com.luckymore.ydd.app.funcs.mainfuncs;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;

import com.base.control.MFragmentAdapter;
import com.base.util.Helper;
import com.base.util.Utils;
import com.luckymore.ydd.app.R;
import com.luckymore.ydd.app.asyncTask.CheckAndUploadTask;
import com.luckymore.ydd.app.db.MQuestionDaoActivity;
import com.luckymore.ydd.app.funcs.funclist.MainFuncsFragment;
import com.luckymore.ydd.app.funcs.notefuncs.NoteFuncsFragment;
import com.luckymore.ydd.app.util.AppConfig;
import com.mobovip.views.DirectionalViewPager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by yjh on 15-3-12.
 */
public class MainFuncsActivity extends MQuestionDaoActivity {
    private DirectionalViewPager viewPager;

    @Override
    protected void create(Bundle savedInstanceState) {
        setContentView(R.layout.activity_mainfuncs);

        List<Fragment> list = new ArrayList<Fragment>(Arrays.asList(
                new NoteFuncsFragment(), new MainFuncsFragment()
        ));

        if(!AppConfig.isFirst) {
            list.add(0, new IndexFragment());
            //2秒后下滑到主功能页一
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewPager.setCurrentItem(1);
                }
            }, 2000);
        }

        viewPager = mFindViewById(R.id.mainfuncs_viewPager);
        viewPager.setAdapter(new MFragmentAdapter(getSupportFragmentManager(), list)); //注入3个fragment

        //设置垂直滑动
        viewPager.setOrientation(DirectionalViewPager.VERTICAL);
        viewPager.setSaveEnabled(false);// else nullpoint when jump

        //有wifi情况下同步
        if(Utils.isWifiConnected(getMActivity()))
            new CheckAndUploadTask(this).execute("");
    }

    //重写系统back键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            Helper.exit(this);
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}
